package com.xhga.transaction.common.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * redis服务类
 * Created by xhga on 2019/4/19.
 */
@Repository
public class RedisService implements RedisApi {

    private static final Logger logger = LoggerFactory.getLogger(RedisService.class);

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public boolean isConnection(){
        try {
            redisTemplate.opsForValue().get("luck");
        } catch (Exception e) {
            logger.info("Redis service is not open.");
            return false;
        }
        logger.info("Redis service is open.");
        return true;
    }

    @Override
    public void add(String key, Object value){
        redisTemplate.opsForValue().set(key, value.toString());
    }

    @Override
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public boolean setNx(String key, String value) {
        return redisTemplate.opsForValue().setIfAbsent(key, value, 100, TimeUnit.MILLISECONDS);
    }

    @Override
    public String get(String key){
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public void increase(String key, int number) {
        logger.info("添加redis数量");
        redisTemplate.opsForValue().increment(key, number);
    }

    @Override
    public int decrement(String key, int number) {
        return redisTemplate.opsForValue().decrement(key, number).intValue();
    }

    @Override
    public void addList(String key, String value) {
        redisTemplate.opsForList().rightPush(key, value);
    }

    @Override
    public void addList(String key, List<String> list) {
        redisTemplate.opsForList().rightPushAll(key,list);
    }

    @Override
    public void setList(String key, int index, String value) {
        redisTemplate.opsForList().set(key, index, value);
    }

    @Override
    public List<String> getList(String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    @Override
    public List<String> getList(String prefix, String key, String name) {
        return getList(prefix + key);
    }

    @Override
    public String pop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    @Override
    public void lock(String key, String value) {
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(key, value, 100, TimeUnit.MILLISECONDS);
        if (aBoolean){
            logger.info("XHGA获得分布式锁:" + key);
            return;
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lock(key, value);
    }

    @Override
    public void unLock(String key) {
        logger.info("XHGA释放分布式锁:" + key);
        redisTemplate.delete(key);
    }
}
